package com.threegroup.admin.controller;

import com.threegroup.admin.common.properties.MinioProperties;
import com.threegroup.admin.common.utils.MinioUtils;
import com.threegroup.admin.utils.AssertUtil;
import com.threegroup.common.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * 文件上传
 */
@RequestMapping("/sys/oss/")
@RestController
@Slf4j
public class FileUploadController {

    @Resource
    private MinioProperties minioProperties;
    @Resource
    private MinioUtils minioUtils;

    @RequestMapping("/upload")
    public Result upLoadFile(MultipartFile file) {
        //获取文件名
        String originalFilename = file.getOriginalFilename();
        int i = originalFilename.lastIndexOf(".");
        String extension = originalFilename.substring(i - 1);//获取后缀
        String fileName = UUID.randomUUID().toString() + extension;//生成新的文件名

        //上传文件
        Boolean upload = minioUtils.upload(file, fileName, minioProperties.getBucketName());
        AssertUtil.isTrue(!upload,303,"文件上传失败");
        return new Result();

    }
}
